package com.app.controller;

import com.app.common.Result;
import com.app.model.MobileModel;
import com.app.server.MobileService;
import com.app.utils.CommonHelp;
import com.app.vo.MobileVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 报灾专线
 */
@Slf4j
@RestController
@RequestMapping("/sbbz/mobile")
public class MobileController {
    @Autowired
    private MobileService mobileService;
    // 查询列表
    @GetMapping("/getMobileList")
    public Result getMobileList(@RequestParam(defaultValue = "0") Integer page,
                                @RequestParam(defaultValue = "10") Integer limit, String keyword) {
        MobileVo mobileList = mobileService.getMobileList(page, limit,keyword);
        return Result.success(mobileList);
    }

    //根据id查询
    @GetMapping("/getMobileById")
    public Result getMobileById(Integer id) {
        MobileModel mobile = mobileService.getById(id);
        return Result.success(mobile);
    }

    // 新增和修改
    @PostMapping("/addAndUpdateMobile")
    public Result addAndUpdateMobile(@RequestBody MobileModel mobileModel) {
        MobileModel mobile = mobileService.getById(mobileModel.getId());
        if (mobile != null) {
            mobileModel.setUpdateTime(CommonHelp.getNowTimestamp());
            mobileService.updateById(mobileModel);
            return Result.success("修改成功");
        } else {
            mobileModel.setCreateTime(CommonHelp.getNowTimestamp());
            mobileService.save(mobileModel);
            return Result.success("添加成功");
        }
    }

    //删除
    @GetMapping("/delMobile")
    public Result delMobile(Integer id) {
        mobileService.removeById(id);
        return Result.success("删除成功");
    }
}
